/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Danhmuc;
import until.HibernateUtil;

/**
 *
 * @author Admin
 */
public class danhMucDAO {
    
    //<editor-fold defaultstate="collapsed" desc="1.Lấy danh mục ">

    public static List<Danhmuc> LayDanhMuc() {
        List<Danhmuc> ds = null;
        Session sc = HibernateUtil.getSessionFactory().openSession();
        try {
            String sql = "from Danhmuc";
            Query query = sc.createQuery(sql);
            ds = query.list();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            sc.close();
        }
        return ds;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="2.Lấy danh mục theo mã">
    public static List<Danhmuc> layDanhMucTheoID(String madanhmuc) {
        List<Danhmuc> dm = null;
        Session sc = HibernateUtil.getSessionFactory().openSession();

        try {
            String hql = "from Danhmuc where ID =" + madanhmuc + "";
            Query query = sc.createQuery(hql);
            dm = query.list();

        } catch (Exception e) {
            System.err.println(e);

        } finally {
            sc.close();
        }
        return dm;
    }
    //</editor-fold>
   
    //<editor-fold defaultstate="collapsed" desc="3.Thêm Danh Mục">
    public static boolean ThemDanhMuc(Danhmuc dm) {
        boolean kq = true;
        Transaction transaction = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            transaction = session.beginTransaction();
            session.save(dm);
            transaction.commit();

        } catch (Exception e) {
            System.out.println(e.getMessage());
            transaction.rollback();
            kq = false;
        } finally {

            session.close();
        }
        return kq;
    }
    //</editor-fold>
   
    //<editor-fold defaultstate="collapsed" desc="4.Xóa Danh Mục">
    public static void XoaDanhMuc(Danhmuc dm) {
        Transaction ts = null;
        Session sesstion = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = sesstion.beginTransaction();
            sesstion.delete(dm);
            ts.commit();
        } catch (Exception e) {
            ts.rollback();
        } finally {
            sesstion.close();
        }

    }
   
   
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="5.Cập nhật danh mục">
    public static void CapNhatDanhMuc(Danhmuc danhmuc) {
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction ts = null;
        try {
            ts = session.beginTransaction();
            session.update(danhmuc);
            ts.commit();
        } catch (Exception e) {
            ts.rollback();
            System.err.println(e);
        } finally {
            session.close();
        }
        
        
    }
    //</editor-fold>
}
